//
//  ActivityPopView.m
//  Cunli
//
//  Created by lilubin on 15/8/20.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ActivityPopView.h"
#import "UIViewExt.h"
#import "UIImageView+WebCache.h"

#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@implementation ActivityPopView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    UIImageView *overlayView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [overlayView setImage:[UIImage imageNamed:@"clearPic"]];
    overlayView.userInteractionEnabled = YES;
    NSLog(@"self.view:%@", NSStringFromCGRect(overlayView.bounds));
    overlayView.backgroundColor = [UIColor clearColor];
    
    CGFloat popViewWidth = kScreenWidth - 20;
    CGFloat popViewHeight = kScreenHeight * 2.0 / 3;
    UIView *popView = [[UIView alloc] initWithFrame:CGRectMake((kScreenWidth-popViewWidth)/2, (kScreenHeight-popViewHeight)/2, popViewWidth, popViewHeight)];
    popView.backgroundColor = [UIColor clearColor];
    popView.layer.shadowPath = [UIBezierPath bezierPathWithRect:popView.bounds].CGPath;
    popView.tag = 2021;
    popView.layer.masksToBounds = NO;
//    popView.layer.shadowOffset = CGSizeMake(5, 5);
    popView.layer.shadowRadius = 5;
//    popView.layer.shadowOpacity = 0.5;
    
    [overlayView addSubview:popView];
    overlayView.tag = 2020;
    overlayView.alpha = 1.0f;
    popView.alpha = 1.0f;
    [self addSubview:overlayView];
    
    CGFloat slideSpace = 8;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(slideSpace, slideSpace, popViewWidth-slideSpace*2, popViewHeight-slideSpace*2)];
    imageView.backgroundColor = [UIColor orangeColor];
    [imageView setImageWithURL:[NSURL URLWithString:_picUrl] placeholderImage:[UIImage imageNamed:@"default_news.png"]];
    imageView.userInteractionEnabled = YES;
    [popView addSubview:imageView];
    [imageView.layer setCornerRadius:8];
    imageView.layer.masksToBounds = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jumpToActivity:)];
    tap.numberOfTapsRequired = 1;
    tap.numberOfTouchesRequired = 1;
    [imageView addGestureRecognizer:tap];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    closeBtn.frame = CGRectMake(popViewWidth-slideSpace*2-6, 0, slideSpace*2+6, slideSpace*2+6);
    [closeBtn setImage:[UIImage imageNamed:@"activityCloseBtn"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeAction:) forControlEvents:UIControlEventTouchUpInside];
    [popView insertSubview:closeBtn aboveSubview:imageView];
    
    CGFloat buttonWidth = 120;
    UIButton *jumpToActivityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    jumpToActivityBtn.frame = CGRectMake(popViewWidth/2-5-60, popViewHeight-50, buttonWidth, 30);
    [jumpToActivityBtn setBackgroundImage:[UIImage imageNamed:@"clearBtnBg1"] forState:UIControlStateNormal];
    [jumpToActivityBtn setTitle:@"点击进入" forState:UIControlStateNormal];
    [jumpToActivityBtn.titleLabel setTextAlignment:NSTextAlignmentCenter];
    [jumpToActivityBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [jumpToActivityBtn addTarget:self action:@selector(jumpToActivity:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:jumpToActivityBtn];
}

- (void)setPicUrl:(NSString *)picUrl
{
    if (picUrl != _picUrl) {
        _picUrl = picUrl;
    }
}

- (void)closeAction:(UIButton *)button
{
    NSLog(@"CLOSE ACTION");
    _closeClicked();
}

- (void)jumpToActivity:(UIButton *)button
{
    NSLog(@"Jump to activity");
    _jumpToActivityClicked();
}

@end
